package Reflect;

import Reflect.annotations.AutoRunClass;
import Reflect.annotations.AutoRunMethod;

import java.io.File;
import java.lang.reflect.Method;

public class Test5 {
    public static void main(String[] args) throws Exception {
        File dir = new File(Test4.class.getResource(".").toURI());
        File[] subs = dir.listFiles(f -> f.getName().endsWith(".class"));
        for (File sub : subs) {
            String[] split = sub.getName().split("\\.");
            String path = Test4.class.getPackage().getName() + "." + split[0];
            Class cls = Class.forName(path);
            if (cls.isAnnotationPresent(AutoRunClass.class)) {
                Object o = cls.newInstance();
                Method[] methods = cls.getDeclaredMethods();
                for (Method method : methods) {
                    if (method.isAnnotationPresent(AutoRunMethod.class)) {
                        int value = method.getAnnotation(AutoRunMethod.class).value();
                        int count = 0;
                        for (int i = 0; i < value; i++) {
                            method.invoke(o);
                            count++;
                        }
                        System.out.println(method.getName() + "被调用了" + count + "次");
                    }
                }
            }
        }
    }
}
